package com.example.f8399.testdemo.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.f8399.testdemo.BR;
import com.example.f8399.testdemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by f8399 on 2018/8/24.
 */

public class RcPageAdaper extends RecyclerView.Adapter<BindingViewHolder> {

    private Context mContext;
    private List<String> mList = new ArrayList<>();
    private LayoutInflater mLayoutInflater;
    private OnItemClickListener mOnItemClickListener;

    public RcPageAdaper(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public RcPageAdaper(Context context, List<String> list) {
        mContext = context;
        mList = list;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public interface OnItemClickListener {
        void onItemClick(String bean);
    }

    @NonNull
    @Override
    public BindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(mLayoutInflater, R.layout.item_rc_pager, parent, false);
        return new BindingViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull BindingViewHolder holder, int position) {
        ViewDataBinding binding = holder.getBinding();
        final String value = mList.get(position);
        binding.setVariable(BR.bean, value);
        binding.setVariable(BR.adaper, this);

        binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(value);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setList(List<String> list) {
        mList = list;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
